﻿using System;
using SdmxMl.Structure;
using SdmxMl.Common;
using SdmxMl.Manager;

namespace SdmxMl.Helper
{
    /// <summary>
    /// Helper for providing internationalized texts based on
    /// SDMX ML ConceptScheme. Each text is uniquely identified by its 
    /// Concept ID.
    /// </summary>
    public class TextProvider
    {
        private ConceptSchemeType cstMessages = null;

        /// <summary> Constructor </summary>
        /// <param name="csPath">Path to SDMX conceptscheme file holding translated texts & descriptions</param>
        public TextProvider(string csPath)
        {
            // Load concept scheme from specified path
            SmManager sm = new SmManager();
            sm.LoadSdmxDocument(csPath, true, null);

            ArtefactManager arm = sm.GetArtefactManager(SdmxArtefactType.Concepts);
            if (arm.ArtefactList.Count == 0)
                throw new ApplicationException("Unexpected: No ConceptScheme found by TextProvider!");
            cstMessages = arm.ArtefactList[0] as ConceptSchemeType;

        }

        /// <summary>Get text matching concept ID </summary>
        /// <param name="lg">specified language for text</param>
        /// <param name="itemId">concept ID</param>
        /// <returns>The text associated to concept in the specified language</returns>
        public string GetTextOf(string lg, string itemId)
        {
            string t = string.Empty;
            ISdmxHierarchyListItem item = cstMessages.ConceptList.LocateItem(itemId);
            if (item != null)
                t = item.Name.GetForLanguage(lg);
            return t;
        }

        /// <summary>Get description matching concept ID </summary>
        /// <param name="lg">specified language to return</param>
        /// <param name="itemId">concept ID</param>
        /// <returns>The description associated to concept in the specified language</returns>
        public string GetDescriptionOf(string lg, string itemId)
        {
            string t = string.Empty;
            ISdmxHierarchyListItem item = cstMessages.ConceptList.LocateItem(itemId);
            ConceptType c = item as ConceptType;
            if (c != null)
                t = c.Description.GetForLanguage(lg);
            return t;
        }

        /// <summary>
        /// Get multilang internatiolized object holding  concept id name
        /// </summary>
        /// <param name="itemId">the concept id</param>
        /// <returns>the multilang container</returns>
        public MultiLang GetInternationalizedTextOf(string itemId)
        {
            MultiLang ml = new MultiLang();
            ISdmxHierarchyListItem item = cstMessages.ConceptList.LocateItem(itemId);
            if (item != null)
                ml = item.Name.Clone();
            return ml;
        }
    }
}
